import { loginByUsername, getUserInfo, getMenuInfo } from '@/api/login'
// import { loginByUsername, logout, getUserInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    user: '',
    status: '',
    code: '',
    token: getToken(),
    name: '',
    avatar: '',
    introduction: '',
    roles: [],
    perms: [],
    setting: {
      articlePlatform: []
    },
    appkey: '',
    appsecret: '',
    companyList: [] // 暂时没用
  },

  mutations: {
    SET_CODE: (state, code) => {
      state.code = code
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_INTRODUCTION: (state, introduction) => {
      state.introduction = introduction
    },
    SET_SETTING: (state, setting) => {
      state.setting = setting
    },
    SET_STATUS: (state, status) => {
      state.status = status
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMS: (state, perms) => {
      state.perms = perms
    },
    SET_APPKEY: (state, appkey) => {
      state.appkey = appkey
    },
    SET_APPSECRET: (state, appsecret) => {
      state.appsecret = appsecret
    },
    SET_COMPANYLIST: (state, companyList) => {
      state.companyList = companyList
    }
  },

  actions: {
    // 用户名登录
    LoginByUsername({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        loginByUsername(userInfo.mobile, userInfo.code).then(response => {
          const token = response.data.data.token
          commit('SET_TOKEN', response.data.data.token)
          if (typeof (Storage) !== 'undefined') {
            localStorage.setItem('userInfo', JSON.stringify(response.data.data.user_info))
            localStorage.setItem('corpType', response.data.data.user_info.corpType)
            localStorage.setItem('appkey', response.data.data.user_info.app_key)
            localStorage.setItem('token', response.data.data.token)
            localStorage.setItem('appSecret', response.data.data.user_info.app_secret)
          }
          // commit('SET_APPKEY', response.data.data.user_info.app_key)
          // commit('SET_APPSECRET', response.data.data.user_info.app_secret)
          setToken(token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetUserInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getUserInfo(state.token).then(response => {
          const data = response.data.data
          const perms = []
          for (var i = 0; i < data.length; i++) {
            perms.push(data[i].name)
          }
          if (perms && perms.length > 0) { // 验证返回的perms是否是一个非空数组
            commit('SET_PERMS', perms)
          } else {
            reject('getInfo: perms must be a non-null array !')
          }

          commit('SET_ROLES', data.roles)
          commit('SET_NAME', data.name)
          commit('SET_AVATAR', data.avatar)
          commit('SET_INTRODUCTION', data.introduction)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取用户信息
    GetMenuInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getMenuInfo(state.token).then(response => {
          // const data = response.data.data
          // console.log(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 第三方验证登录
    // LoginByThirdparty({ commit, state }, code) {
    //   return new Promise((resolve, reject) => {
    //     commit('SET_CODE', code)
    //     loginByThirdparty(state.status, state.email, state.code).then(response => {
    //       commit('SET_TOKEN', response.data.token)
    //       setToken(response.data.token)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },

    // 登出
    LogOut({ commit, state }) {
      // return new Promise((resolve, reject) => {
      //   logout(state.token).then(() => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      commit('SET_PERMS', [])
      // commit('SET_APPSECRET', '')
      // commit('SET_APPKEY', '')
      if (typeof (Storage) !== 'undefined') {
        localStorage.setItem('appkey', '')
        localStorage.setItem('appSecret', '')
      }
      removeToken()
      // resolve()
      //   }).catch(error => {
      //     reject(error)
      //   })
      // })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        // commit('SET_APPSECRET', '')
        // commit('SET_APPKEY', '')
        if (typeof (Storage) !== 'undefined') {
          localStorage.setItem('appkey', '')
          localStorage.setItem('appSecret', '')
        }
        removeToken()
        resolve()
      })
    },

    // 动态修改权限
    ChangeRoles({ commit, dispatch }, role) {
      return new Promise(resolve => {
        commit('SET_TOKEN', role)
        setToken(role)
        getUserInfo(role).then(response => {
          const data = response.data
          const perms = []
          for (var i = 0; i < data.length; i++) {
            perms.push(data[i].name)
          }
          commit('SET_ROLES', data.roles)
          commit('SET_PERMS', perms)
          commit('SET_NAME', data.name)
          commit('SET_AVATAR', data.avatar)
          commit('SET_INTRODUCTION', data.introduction)
          dispatch('GenerateRoutes', perms) // 动态修改权限后 重绘侧边菜单
          resolve()
        })
      })
    }
  }
}

export default user
